package cz.pproproj.aaservice.ddbmodule;

import cz.pproproj.aaservice.ddbmodule.dbaccess.DBUsersManagement;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @author David
 * @since 14. 12. 2014
 */
public class DataDBModuleImpl {

    private static final DataDBModuleImpl INSTANCE = new DataDBModuleImpl();

    private Map<String, DBUsersManagement> _dbUsersManagement = new HashMap<>();

    private DataDBModuleImpl() {
    }

    /**
     * Method which returns singleton instance of class {@link cz.pproproj.aaservice.ddbmodule.DataDBModuleImpl}
     * @return instance of {@link cz.pproproj.aaservice.ddbmodule.DataDBModuleImpl}
     */
    public static DataDBModuleImpl getInstance() {
        return INSTANCE;
    }

    /**
     * Method which allow access to methods which manipulate with users in DB.
     * Class which is returned is set in dataDbModuleContext.xml in bean with id usersManagement
     * @return instance of class which implements {@link cz.pproproj.aaservice.ddbmodule.dbaccess.DBUsersManagement}
     * @param configFile
     */
    public DBUsersManagement getDbUsersManagement(String configFile) {
        DBUsersManagement dbUManagement = _dbUsersManagement.get(configFile);
        if (dbUManagement == null) {
            ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
            dbUManagement = (DBUsersManagement) context.getBean("usersManagement");
            _dbUsersManagement.put(configFile, dbUManagement);
        }
        return dbUManagement;
    }
}
